package lc.二叉树.剑指Offer;

import utils.TreeNode;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/26
 */
public class 剑指Offer54_二叉搜索树的第k大节点 {

    int ans = 0,num = 0;
    public int kthLargest(TreeNode root, int k) {
        dfs(root,k);
        return ans;
    }

    private void dfs(TreeNode node, int k) {

        if (node.right != null)
            dfs(node.right,k);

        if (++num == k) {
            ans = node.val;
        }
        if (node.left != null)
            dfs(node.left ,k);
    }
}
